package com.example.administrator.myapplication;

import android.content.Context;
import android.os.Bundle;
import android.service.quicksettings.Tile;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Administrator on 2020/11/12.
 */

public class CommonDialog extends AlertDialog {

    private TextView titleTv;
    private TextView contentTv;
    private Button leftBtn;
    private Button rightBtn;
    private String title;
    private String content;
    private String positive;
    private String negtive;


    protected CommonDialog(@NonNull Context context) {
        super(context);

    }
    //自定义对话框加载对应的视图
    //只要有需要显示的视图，需加载对应的布局文件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog);
        initView();
        initEvent();
    }
    private void initView(){
        titleTv = findViewById(R.id.custom_title_tv);
        contentTv = findViewById(R.id.custom_content_tv);
        leftBtn = findViewById(R.id.custom_left_bt);
        rightBtn = findViewById(R.id.custom_right_bt);
    }
    private void refreshView(){
        //如果定义ｔｉｔｌｅ和ｃｏｎｔｅｎｔｄｅ的内容，则会在弹出框显示
        if(!TextUtils.isEmpty(title)){
            titleTv.setText(title);
            titleTv.setSystemUiVisibility(View.VISIBLE);//显示
    }else {
            titleTv.setVisibility(View.GONE);//隐藏
        }
        if(!TextUtils.isEmpty(content)){
            contentTv.setText(content);
            contentTv.setVisibility(View.VISIBLE);
        }else {
            contentTv.setVisibility(View.GONE);
        }
        if(!TextUtils.isEmpty(positive)){
            rightBtn.setText(positive);
        }else {
            rightBtn.setText("确定");
        }
        if(!TextUtils.isEmpty(negtive)) {
            leftBtn.setText(negtive);
        }else {
              leftBtn.setText("取消");
          }

    }

    //等同于initData（）
    private void initEvent(){
        leftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            if (onClickBottomListener !=null){
                onClickBottomListener.onLeftBottomClick();
            }
            }
        });
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
           if(onClickBottomListener != null){
               onClickBottomListener.onRightBottomClick();
           }
            }
        });
    }

    @Override
    public void show() {
        super.show();
        refreshView();
    }

    //第一步　提供接口
    public interface OnClickBottomListener{
       void onLeftBottomClick();
       void onRightBottomClick();

    }
    //设置“确定”“取消” 按钮的回调
    public OnClickBottomListener onClickBottomListener;

    //第二步 提供设置监听事件的方法，并提供回调方法
    public CommonDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener){
        this.onClickBottomListener =onClickBottomListener;
        return  this;
     }
     public CommonDialog setPositive(String positive){
        this.positive =positive;
        return this;
     }
     public CommonDialog setNegtive(String negtive) {
         this.negtive = negtive;
         return this;
     }

    public CommonDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public CommonDialog setContent(String content) {
        this.content = content;
        return this;
    }





}


